package com.vhly.remoteapi.yahoo.contact.field;

import com.vhly.remoteapi.yahoo.contact.FieldValue;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-13
 * Time: 22:21:43
 * Email: vhly@163.com
 */

/**
 * Single Content Field<br/>
 * Email
 * JobTitle
 * Link
 * NickName
 * Phone
 */
public class TextField extends FieldValue {

    private String content;

    public TextField(String text){
        content = text;
    }

    /**
     * read object from DataInput
     *
     * @param din DataInput
     */
    @Override
    public void readObject(DataInput din) throws IOException {
        byte b = din.readByte();
        if(b == 1){
            content = din.readUTF();
        }
    }

    /**
     * Write object to DataOutput
     *
     * @param dout DataOutput
     */
    @Override
    public void writeObject(DataOutput dout) throws IOException {
        if(content != null){
            dout.writeByte(1);
            dout.writeUTF(content);
        }else{
            dout.writeByte(0);
        }
    }

    /**
     * Serial content to XML
     *
     * @return String
     */
    @Override
    public String toXML() {
        return content;
    }

    /**
     * Parse Element
     *
     * @param el Element
     */
    @Override
    public void parse(Element el) {
        NodeList cns = el.getChildNodes();
        if(cns != null && cns.getLength() > 0){
            Text txt = (Text) cns.item(0);
            content = txt.getNodeValue();
        }
    }

    @Override
    public String toString() {
        return content;
    }
}
